package com.devunion.reader.client.core;

import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.ContainerEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.*;


/**
 * @author Timoshenko Alexander 28.05.2009 : 10:01:54
 */
public class AppViewport extends Viewport {
    public static String CLASS_NAME = "AppViewport.class";

    private Listener<ContainerEvent> layout = new Listener<ContainerEvent>() {
        public void handleEvent(ContainerEvent containerEvent) {
            ((ContentPanel) containerEvent.container).layout();
        }
    };

    private Listener<ContainerEvent> afterLayout = new Listener<ContainerEvent>() {
        public void handleEvent(ContainerEvent containerEvent) {
            ContentPanel panel = ((ContentPanel) containerEvent.container);

            if (panel.getWidget(0) == null) {
                ((BorderLayout) getLayout()).hide(getLayotRegion(panel));
            } else {
                if (!panel.isVisible()) {
                    ((BorderLayout) getLayout()).show(getLayotRegion(panel));
                }
            }
        }
    };

    private ContentPanel north = new ContentPanel(new FitLayout());
    private ContentPanel center = newInstance();
    private ContentPanel west = newInstance();
    private ContentPanel east = newInstance();

    public AppViewport() {
        setLayout(new BorderLayout());
        setStyleAttribute("backgroundColor", "white");

        BorderLayoutData northLayoutData = new BorderLayoutData(Style.LayoutRegion.NORTH, 250);
        northLayoutData.setMargins(new Margins(0, 0, 5, 0));
        add(north, northLayoutData);

        BorderLayoutData centerLayoutData = new BorderLayoutData(Style.LayoutRegion.CENTER);
        centerLayoutData.setMargins(new Margins(5, 0, 0, 0));
        add(center, centerLayoutData);

        BorderLayoutData westLayoutData = new BorderLayoutData(Style.LayoutRegion.WEST);
        westLayoutData.setMargins(new Margins(5, 0, 0, 0));
        add(west, westLayoutData);

        BorderLayoutData eastLayoutData = new BorderLayoutData(Style.LayoutRegion.EAST);
        eastLayoutData.setMargins(new Margins(5, 0, 0, 0));
        add(east, eastLayoutData);

        Registry.register(CLASS_NAME, this);
    }

    public ContentPanel getPanel(Style.LayoutRegion region) {
        if (region == Style.LayoutRegion.WEST) {
            return west;
        } else if (region == Style.LayoutRegion.CENTER) {
            return center;
        } else if (region == Style.LayoutRegion.EAST) {
            return east;
        } else if (region == Style.LayoutRegion.NORTH) {
            return north;
        }
        return center;
    }

    private Style.LayoutRegion getLayotRegion(LayoutContainer container) {
        if (container == north) {
            return Style.LayoutRegion.NORTH;
        } else if (container == west) {
            return Style.LayoutRegion.WEST;
        } else if (container == center) {
            return Style.LayoutRegion.CENTER;
        } else if (container == east) {
            return Style.LayoutRegion.EAST;
        }

        return null;
    }

    public void clear(Style.LayoutRegion region) {
        if (region == Style.LayoutRegion.WEST) {
            west.removeAll();
        } else if (region == Style.LayoutRegion.CENTER) {
            center.removeAll();
        } else if (region == Style.LayoutRegion.EAST) {
            east.removeAll();
        }
    }

    public void clearAll() {
        center.removeAll();
        west.removeAll();
        east.removeAll();
    }

    private ContentPanel newInstance() {
        ContentPanel result = new ContentPanel(new RowLayout(Style.Orientation.VERTICAL));
        result.setBodyBorder(false);
        result.setHeaderVisible(false);
        result.setStyleAttribute("backgroundColor", "white");
        result.setScrollMode(Scroll.AUTO);

        result.addListener(Events.Add, layout);
        result.addListener(Events.AfterLayout, afterLayout);
        return result;
    }
}
